﻿using Microsoft.Office.Interop.Excel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ExcelAutomationRunner
{

    public class CSV_to_XLSX_Converter : ExcelApplicationBase
    {

        private string _renamedFilePath;  // in case .txt passed in, for example.
        private string _inputFilePath;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="inputFilePath">Extension aside, it's expected to be a CSV file.</param>
        /// <returns>Success, file path (or) Failure, error message</returns>
        public Tuple<bool, string> CSV_to_XLSX( string inputFilePath )
        {
            _renamedFilePath = null;
            _inputFilePath = inputFilePath;
            if ( RenameFile( inputFilePath ) )
            {
                return ProcessApplication();  // calls Process below.
            }
            else
            {
                return new Tuple<bool, string>( false, Constants.UNABLE_TO_RENAME_FILE );
            }
        }

        protected override Tuple<bool, string> Process( Application excel )
        {
            var result = DoCSV_to_XLSXConversion( _renamedFilePath ?? _inputFilePath, excel );
            if ( File.Exists( _renamedFilePath ) )
            {
                File.Delete( _renamedFilePath );
            }
            return result;
        }

        /// <summary>
        /// If the extension is csv, sol good; Excel will figger it out.
        /// </summary>
        /// <param name="inputFilePath"></param>
        /// <returns></returns>
        private bool RenameFile( string inputFilePath )
        {
            if ( string.Compare( Path.GetExtension( inputFilePath ), Constants.CSV_EXTENSION, true ) != 0 )
            {
                var newPath = Path.ChangeExtension( inputFilePath, Constants.CSV_EXTENSION );
                if ( File.Exists( newPath ) )
                {
                    return false;
                }
                else
                {
                    File.Copy( inputFilePath, newPath );
                    _renamedFilePath = newPath;
                    return true;
                }
            }
            else
            {
                return true;
            }
        }

        /// <summary>
        /// Using EPPlus to do this is not easy.  Other packages exist now, but if you have Excel installed, this is the quickest easiest way.
        /// </summary>
        /// <param name="csvFilePath"></param>
        /// <param name="excel"></param>
        /// <returns></returns>
        private Tuple<bool, string> DoCSV_to_XLSXConversion( string csvFilePath, Application excel )
        {
            _newPath = Path.ChangeExtension( csvFilePath, Constants.EXCEL_EXTENSION );
            if ( File.Exists( _newPath ) )
            {
                return new Tuple<bool, string>( false, string.Format( Constants.CONVERTED_FILE_ALREADY_EXISTS, _newPath ) );
            }
            else
            {
                excel.Workbooks.OpenText( csvFilePath );
                excel.Workbooks[1].SaveAs( _newPath, XlFileFormat.xlWorkbookDefault );  // Everything 1 based
                excel.Workbooks[1].Close();
                if ( File.Exists( _newPath ) )
                {
                    return new Tuple<bool, string>( true, _newPath );
                }
                else
                {
                    return new Tuple<bool, string>( false, Constants.UNKNOWN_CONVERSION_FAILURE );
                }
            }
        }

    }
}
